<template>
  <div class="editInfo">
    <!-- 子触发父方法
              1:子组件标签上绑定一个方法  @子组件方法名="父组件方法"
              2:子组件内触发父组件方法
                 this.$emit("子组件方法名",传参)      
       -->
    <hmNavBar
      :title="titleObj[type]"
      :path="path"
      :right="right"
      @rightClick="save"
    ></hmNavBar>
    <div class="content">
      <van-field
        v-if="type !== 'avatar'"
        class="field"
        v-model="inputValue"
      ></van-field>
      <van-uploader
        v-else
        v-model="imgArr"
        max-count="1"
        :before-read="beforeRead"
        :after-read="afterRead"
        @delete="deleteEvent"
      ></van-uploader>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import { auEdit, upload } from '@/api/my'
export default {
  data () {
    return {
      inputValue: '',
      right: '',
      type: this.$route.query.type,
      path: '/home/myInfo',
      titleObj: {
        nickname: '修改昵称',
        intro: '修改个人简介',
        position: '修改岗位',
        avatar: '修改头像'
      },
      imgArr: []
    }
  },
  // 使用场景:依赖一个或者多个值产生一个新的值 data再来有computed
  computed: {
    ...mapState(['userInfo'])
  },
  watch: {
    /*
    侦听器:某个值(能通过this访问)的change事件
    方法名:值的名字(写出完整的值,去掉this.加上引号)
    方法名(newVal,oldVal){
       newVal:当前值
       oldVal:修改上一刻的值
    }
    */
    inputValue (newVal) {
      // 如果有修改
      if (newVal !== this.userInfo[this.type] && newVal !== '') {
        this.right = '保存'
      } else {
        this.right = ''
      }
    }
  },
  created () {
    this.inputValue = this.userInfo[this.type]
    if (this.type === 'position') {
      this.path = '/home/my'
    }
    if (this.type === 'avatar' && this.userInfo.avatar) {
      this.imgArr[0] = { url: this.$baseUrl + this.userInfo.avatar }
    }
  },
  methods: {
    async save () {
      this.$toast.loading({ duration: 0 })
      //   对象的key希望是变量  {[key]:value}
      await auEdit({
        [this.type]: this.inputValue
      })
      this.$toast.success('修改资料成功')
      this.$router.push(this.path)
      this.$store.dispatch('refreshUserInfo')
    },
    // 读取前
    beforeRead (file) {
      const _type = file.type === 'image/png' || file.type === 'image/jpeg'
      const _size = file.size / 1024 / 1024 < 2
      if (!_type) {
        this.$toast.fail('请上传png或者jpeg格式图片')
      }
      if (!_size) {
        this.$toast.fail('请上传小于2M的图片')
      }
      return _type && _size
    },
    // 读取后
    async afterRead (res) {
      // res.content  res.file
      const _formData = new FormData() // 不能修改栈
      _formData.append('files', res.file)
      this.$toast.loading({
        duration: 0,
        message: '上传中...'
      })
      const res2 = await upload(_formData)
      this.inputValue = res2.data.data[0].id
      this.$toast.success('上传成功')
      window.console.log(res2)
    },
    // 删除图片时触发
    deleteEvent () {
      this.inputValue = '' // 不显示保存
    }
  }
}
</script>
<style lang="less" scoped>
.editInfo {
  .content {
    padding: 34px @p15 0;
    text-align: center;
    .field {
      background-color: #f7f4f5;
    }
  }
}
</style>
